package com.barbeque.mall.controller.wx;

import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.wx.search.WxKeyword;
import com.barbeque.mall.bean.wx.search.WxMarketKeyword;
import com.barbeque.mall.bean.wx.search.WxMarketSearchHistory;
import com.barbeque.mall.service.wx.WxSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("wx/search")
public class WxSearchController {
    @Autowired
    WxSearchService wxSearchService;

    @RequestMapping("index")
    public BaseRespVo index() {
        WxKeyword wxKeyword = new WxKeyword();
        WxMarketKeyword defaultKeyword = wxSearchService.selectDefaultKeyword();
        wxKeyword.setDefaultKeyword(defaultKeyword);

        List<WxMarketKeyword> wxMarketKeywordList = wxSearchService.selectHotKeywordList();
        wxKeyword.setHotKeywordList(wxMarketKeywordList);

        List<WxMarketSearchHistory> historyKeyword =  wxSearchService.selectHistoryKeyword();
//        ArrayList<String> strings = new ArrayList<>();
//        for (WxMarketSearchHistory wxMarketSearchHistory : historyKeyword) {
//            strings.add(wxMarketSearchHistory.getKeyword());
//        }
//        List<String> history = strings;
        wxKeyword.setHistoryKeywordList(historyKeyword);

        BaseRespVo<WxKeyword> wxKeywordBaseRespVo = new BaseRespVo<>();
        wxKeywordBaseRespVo.setErrmsg("成功");
        return BaseRespVo.ok(wxKeyword);
    }

    @RequestMapping("helper")
    public BaseRespVo helper(@RequestParam String keyword) {
        List<String> goodsName = wxSearchService.getGoodsName(keyword);
        wxSearchService.insertKeyword(keyword);
        BaseRespVo<String> stringBaseRespVo = new BaseRespVo<>();
        stringBaseRespVo.setErrmsg("成功");
        return BaseRespVo.ok(goodsName);
    }

    @RequestMapping("clearhistory")
    public BaseRespVo clearhistory(){
        wxSearchService.clearHistory();
        return BaseRespVo.ok(null);
    }
}
